﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VIBlend.WPF.Controls;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace AnonsaiWPF.CSharp.ComboBoxDemos
{
    public partial class ComboBoxBinding : Window, IWindow
    {
        public ComboBoxBinding()
        {
            InitializeComponent();

            if (this.viblendCombo == null)
                return;

            GenerateSampleData();
            this.viblendCombo.SelectedIndex = 0;
        }

        private void BindCombo()
        {
            if (this.viblendCombo == null)
                return;

            GenerateSampleData();
            this.viblendCombo.SelectedIndex = 0;
        }

        ObservableCollection<Person> sampleList;

        private void GenerateSampleData()
        {
            #region SampleData
            string[] firstNames = new string[] 
                {
                    "Andrew",
                    "Nancy",
                    "Shelley",
                    "Regina",
                    "Yoshi",
                    "Antoni",
                    "Mayumi",
                    "Ian",
                    "Peter",
                    "Lars",
                    "Petra",
                    "Martin",
                    "Sven",
                    "Eric",
                    "Elio",
                    "Beate",
                    "Cheryl",
                    "Michael",
                    "Ivan",
                    "Mikhail",
                    "Maria",
                    "Lucas",
                    "Monica",
                    "Britney",
                    "Christina",
                    "John",
                    "George",
                    "Guylène",
                    "Mark",
                    "Mohan",
                    "Tristan",
                };

            string[] lastNames = new string[] 
                {
                    "Fuller",
                    "Davolio",
                    "Burke",
                    "Murphy",
                    "Nagase",
                    "Saavedra",
                    "Ohno",
                    "Devling",
                    "Wilson",
                    "Peterson",
                    "Winkler",
                    "Bein",
                    "Petersen",
                    "Rossi",
                    "Vileid",
                    "Saylor",
                    "Björn",
                    "Prashan",
                    "Nodier",
                    "Spears",
                    "Milenkovic",
                    "Jovanovic",
                    "Kumar",
                    "Wei",
                    "Nelson",
                    "Duncan",
                    "Bianchi",
                    "Murdock",
                    "Lating",
                    "O'Connor",
                    "Sweet",
                };
            #endregion
   
            sampleList = new ObservableCollection<Person>();

            this.viblendCombo.DisplayMemberPath = "Name";
            this.viblendCombo.ItemsSource = sampleList;
            Random rand = new Random();
            List<string> names = new List<string>();
            for (int i = 0; i < 200; i++)
            {
                string record =
                        firstNames[rand.Next(0, firstNames.Length - 1)] + " " +
                        lastNames[rand.Next(0, lastNames.Length - 1)];

                Person person = new Person() { Id = i, Name = record };

                if (!names.Contains(record))
                {
                    names.Add(record);
                    sampleList.Add(person);
                }
            }

            this.viblendCombo.SelectedItem = sampleList[0];
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.BindCombo();
        }

        private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.comboBox2 == null)
                return;

            switch (comboBox2.SelectedIndex)
            {
                case 0:
                    this.viblendCombo.FilterMode = EditFilterMode.Contains;
                    break;
                case 1:
                    this.viblendCombo.FilterMode = EditFilterMode.StartsWith;
                    break;
            }

        }

        bool isAscending;
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            isAscending = !isAscending;

            ListSortDirection sortDirection = isAscending ? ListSortDirection.Ascending : ListSortDirection.Descending;
            this.viblendCombo.Sort(sortDirection);
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            this.viblendCombo.RemoveSort();
            this.isAscending = false;
        }

        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public void SetTheme()
        {
        }

        public Grid Root
        {
            get
            {
                return this.RootVisual;
            }
        } 
    }

    /// <exclude/>
    public class Person
    {
        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>
        /// The id.
        /// </value>
        public int Id
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>
        /// The name.
        /// </value>
        public string Name
        {
            get;
            set;
        }
    }

}
